﻿using famails.common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace famails.App_Code.source.common
{
    public class TrackingUtils
    {
        public enum sendStatus : int
        {
            Sent = -1, Delivery = 0, Bounce = 1, Complain = 2, NotSend = 3
        }
        static log4net.ILog logs_traching = log4net.LogManager.GetLogger("TrackingEmailAppender");
        public static void TrackingItem(string _event, string messageId, string from, string to)
        {
            string sql = "";
            switch (_event)
            {
                case "bounced":
                    sql = @"update tblSendMail set sendstatus=" + (int)sendStatus.Bounce + " where messageid like'%" + messageId.Split(',')[0] + "%'";
                    break;
                case "delivered":
                    sql = @"update tblSendMail set sendstatus=" + (int)sendStatus.Delivery + " where messageid like'%" + messageId + "%'";
                    break;
                case "opened":
                    sql = @"update tblSendMail set sendstatus=" + (int)sendStatus.Delivery + ", isOpen=1, OpenDate=getdate() where  messageid like'%" + messageId + "%'";
                    break;
                case "clicked":
                    sql = @"update tblSendMail set sendstatus=" + (int)sendStatus.Delivery + ", isClick=1 where messageid like'%" + messageId + "%'";
                    break;
                case "dropped":
                    sql = @"update tblSendMail set sendstatus=" + (int)sendStatus.Bounce + " where  messageid like'%" + messageId + "%'";
                    break;
            }

            SqlCommand cmd = new SqlCommand(sql,
                ConnectionData._MyConnection);
            if (ConnectionData._MyConnection.State != ConnectionState.Open)
                ConnectionData._MyConnection.Open();
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            cmd.Dispose();

            if (_event == "bounced" || _event == "dropped")
            {
                cmd = new SqlCommand("SP_LogBounceEmail", ConnectionData._MyConnection);
                if (ConnectionData._MyConnection.State != ConnectionState.Open)
                    ConnectionData._MyConnection.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@FromEmail", SqlDbType.NVarChar, 250)).Value = from;
                cmd.Parameters.Add(new SqlParameter("@ToEmail", SqlDbType.NVarChar, 250)).Value = to;
                cmd.Parameters.Add(new SqlParameter("@SendTime", SqlDbType.NVarChar, 250)).Value = _event;
                cmd.Parameters.Add(new SqlParameter("@BounceType", SqlDbType.NVarChar, 250)).Value = _event;
                cmd.ExecuteNonQuery();
            }

            logs_traching.Info("event:"+_event+" messageId:"+messageId);
        }
    }
}